package com.kiell.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.kiell.gulimall.product.entity.AttrEntity;
import com.kiell.gulimall.product.service.AttrAttrgroupRelationService;
import com.kiell.gulimall.product.service.AttrService;
import com.kiell.gulimall.product.service.CategoryService;
import com.kiell.gulimall.product.vo.AttrGroupRelationVo;
import com.kiell.gulimall.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.kiell.gulimall.product.entity.AttrGroupEntity;
import com.kiell.gulimall.product.service.AttrGroupService;
import com.kiell.common.utils.PageUtils;
import com.kiell.common.utils.R;



/**
 * 属性分组
 *
 * @author kiell
 * @email kiell@gmail.com
 * @date 2023-09-06 17:24:39
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    /**
     * 获取分类下所有分组&关联属性
     * /product/attrgroup/{catelogId}/withattr
     */
    @RequestMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("catelogId") Long catelogId){
        //1.查出当前分类下的所有属性分组
        List<AttrGroupWithAttrsVo> vos = attrGroupService.getAttrGroupWithAttrsByCatelogId(catelogId);
        //2.查出每个属性分组的所有属性
        //3.整合数据

        return R.ok().put("data",vos);
    }

    /**
     * 添加属性关联分组
     * /product/attrgroup/attr/relation
     */
    @RequestMapping("/attr/relation")
    public R addRelation(@RequestBody List<AttrGroupRelationVo> vos){
        attrAttrgroupRelationService.saveBatch(vos);

        return R.ok();
    }

    /**
     * 查询分组关联的列表
     * /product/attrgroup/{attrgroupId}/attr/relation
     */
    @RequestMapping("/{attrgroupId}/attr/relation")
    public R relation(@PathVariable("attrgroupId") Long attrgroupId){
        List<AttrEntity> entities = attrService.getAttrRelation(attrgroupId);

        return R.ok().put("data", entities);
    }

    /**
     * 查询没有被分组关联的列表
     * /product/attrgroup/{attrgroupId}/noattr/relation
     */
    @RequestMapping("/{attrgroupId}/noattr/relation")
    public R noRelation(@RequestParam Map<String, Object> params, @PathVariable("attrgroupId") Long attrgroupId){
        PageUtils page = attrService.getNoAttrRelation(params,attrgroupId);

        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params, @PathVariable("catelogId") Long catelogId){
//        PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params,catelogId);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long catelogId = attrGroup.getCatelogId();
        Long[] catelogPath = categoryService.findCatelogPath(catelogId);
        attrGroup.setCatelogPath(catelogPath);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 移除销售属性关联分组关系
     * /product/attrgroup/attr/relation/delete
     */
    @RequestMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrGroupRelationVo[] vos) {
        attrService.deleteRelation(vos);

        return R.ok();
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
